﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExampleCallNoWin
{

    public partial class Main : Form
    {
        protected int _TargetProcessID = 0;

        public Main()
        {
            InitializeComponent();
        }

        private void BtnGetProcessID_Click(object sender, EventArgs e)
        {
            Process[] pss = Process.GetProcessesByName("MainWinform");
            if(pss!=null && pss.Length>0)
            {
                //主意要使用线程ID而非进程ID
                _TargetProcessID = pss[0].Threads[0].Id;
                Console.WriteLine("get " + _TargetProcessID);
            }else
            {
                Console.WriteLine("No get");
            }
        }

        private void BtnSendMsg_Click(object sender, EventArgs e)
        {
            if(_TargetProcessID!=0)
            {
                bool b = Shell.PostMessage(_TargetProcessID, 0x8002);
                Console.WriteLine("Post "+b);
            }
        }
    }
}
